package com.jwdf.uda.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.QuizVoterListAction;
import com.jwdf.uda.ui.adapter.VoteOptionAdapter;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.ui.fragment.base.SimpleListFragment;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.view.XListView;

import retrofit2.Response;

public class VoteOptionListActivity extends AbsTitleBarActivity {
    private final static String EXTRA_ID = "id";
    private final static String EXTRA_OPTION_ID = "optionid";
    private int voteId;
    private int optionId;
    private String optionTitle = "";
    public static void startActivity(Context context, int voteId, int optionId) {
        Intent intent = new Intent(context, VoteOptionListActivity.class);
        intent.putExtra(EXTRA_ID, voteId);
        intent.putExtra(EXTRA_OPTION_ID, optionId);
        context.startActivity(intent);
    }
    @Override
    protected String getTopTitle() {
        return getString(R.string.title_vote_option, optionTitle);
    }
    @Override
    protected BaseFragment onCreateContentFragment() {
        return new VoteOptionListFragment();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        voteId = getIntent().getIntExtra(EXTRA_ID, 0);
        optionId = getIntent().getIntExtra(EXTRA_OPTION_ID, 0);
        PbBean.PbSignBaseVoteOptionType optionList = PbBean.PbSignBaseVoteOptionType.valueOf(optionId);
        switch (optionList) {
            case VOTE_OPTION_TYPE_A:
                optionTitle = getString(R.string.title_vote_plan_a);
                break;
            case VOTE_OPTION_TYPE_B:
                optionTitle = getString(R.string.title_vote_plan_b);
                break;
            case VOTE_OPTION_TYPE_C:
                optionTitle = getString(R.string.title_vote_plan_c);
                break;
            case VOTE_OPTION_TYPE_D:
                optionTitle = getString(R.string.title_vote_plan_d);
                break;
        }
        super.onCreate(savedInstanceState);
    }
    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putInt(EXTRA_ID, voteId);
        args.putInt(EXTRA_OPTION_ID, optionId);
        return args;
    }
    public static class VoteOptionListFragment extends SimpleListFragment {
        private VoteOptionAdapter mAdapter;
        private int voteId;
        private int optionId;

        @Override
        protected void initViews() {
            voteId = getArguments().getInt(EXTRA_ID);
            optionId = getArguments().getInt(EXTRA_OPTION_ID);

            mAdapter = new VoteOptionAdapter(getActivity());
            mDataBinding.commonFragmentList.setAdapter(mAdapter);
            mDataBinding.commonFragmentList.setXListViewListener(new XListView.IXListViewListener() {
                @Override
                public void onRefresh(XListView xListView) {
                    requestData(true);
                }

                @Override
                public void onLoadMore(XListView xListView) {
                    requestData(false);
                }
            });
        }

        @Override
        protected void onListItemClick(int position) {

        }

        @Override
        protected void requestData(final boolean refresh) {
            if (refresh && mAdapter.isEmpty()) {
                showProgressDialog();
            }
            int offset = refresh ? 0 : mAdapter.getCount();
            onNewRequestCall(QuizVoterListAction.newInstance(getActivity(), voteId, PbBean.PbSignBaseVoteOptionType.valueOf(optionId), offset).request(new AHttpService.IResCallback<PbBean.PbResQuestionVoterList>() {
                @Override
                public void onCallback(int resultCode, Response<PbBean.PbResQuestionVoterList> response, PbBean.PbBaseServerInfo baseServerInfo) {
                    if (getView() != null) {
                        closeProgressDialog();
                        String errorMsg = HttpHelper.showErrorMsg(getContext(), resultCode, baseServerInfo, getString(R.string.error_net));
                        if (!StringUtils.isNullEmpty(errorMsg)) {
                            mDataBinding.commonFragmentList.stopRefresh();
                            mDataBinding.commonFragmentList.stopLoadMore();
                            return;
                        }
                        doSuccessResponse(refresh, response.body());
                    }
                }
            }));
        }

        private void doSuccessResponse(boolean refresh, PbBean.PbResQuestionVoterList body) {
            if (body != null) {
                if (refresh) {
                    mAdapter.setData(body.getVoterInfoList());
                } else {
                    mAdapter.append(body.getVoterInfoList());
                }
                mDataBinding.commonFragmentList.setPullLoadEnable(body.getVoterInfoList().size() == getPageLength());
                mDataBinding.commonFragmentList.stopRefresh();
                mDataBinding.commonFragmentList.stopLoadMore();
            }
        }
    }
}
